var app = getApp();
var util = require('../../utils/util.js');
import { dealers } from '../../utils/tabBarUrl'
import {date,years,months,days,today} from '../../utils/date';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content:'',
    isopera: false,
    isInput:false, //键盘弹窗
    KeyboardKeys: [1, 2, 3, 4, 5, 6, 7, 8, 9, '·', 0], //键盘
    bar_List:['采购池','采购单','待收货','待终审','对账','已完成'],
    leftChoose: -1,
    toView: "b0",  //瞄点索引初始值
    is_checked: {},
    isclasscheck: {}, //类选是否选中
    isChooseAll: false,
    shop_ids: [], //选中商品id数组
    shop_good_ids:[], //选中商品的good_id数组
    imgs: [], //选中商品img数组
    true_nums:[],  //实际采购量num数组
    checkCount:0,
    page:1,
    page_count:1,
    load:'加载中...',
    loading:1,  //正在加载
    isshou_hint:false, //收货提醒
    is_check_hint:false, //审批提醒
    screen_type_list1:{'1':'待接单','4':'待收货'},
    screen_type_list2:{'7':'已入库','6':'取消','rt2':'已退单'},
    z_screen_type_index:-1001,
    screen_type_index:-1001,
    screen_area_list:['过去一天','过去一周','过去一月'],
    z_screen_area_index:-1,
    screen_area_index:-1,
    area_time:0, //时间段开始时间
    is_screen:false,  //筛选下拉
    z_choose_supplier_id:0,   //筛选供应商id
    z_choose_supplier_name:'',
    choose_supplier_id:0,  //筛选供应商id
    // 时间周期********
    years,
    year: date.getFullYear(),
    months:months,
    month: 2,
    days:days,
    day: 2,
    value: [date.getFullYear() - 2010, date.getMonth(), date.getDate() - 1],  //默认今天
    time_type: 0,  //时间time切换的初始索引值
    start_time: 0,  //开始时间
    today:today, //今天
    end_time:today,    //结束时间,默认今天
    z_start_time:0, //选择开始时间
    z_end_time:today,   //选择结束时间,默认今天
    c_start_time: 0,  //右窗开始时间
    c_end_time:today //结束时间,默认今天
    // *****
  },
  /**
   * 生命周期函数--监听页面加载
  */
  onLoad(options){
    var wpx1 = 240/750 * wx.getSystemInfoSync().windowWidth;
    var wpx2 = 200/750 * wx.getSystemInfoSync().windowWidth;
    this.setData({
      loginInfo:app.globalData.loginInfo,
      use_person_id:app.globalData.person_id, //当前用户的个人id
      raplace_h:0.7*wx.getSystemInfoSync().windowHeight,
      height1:wx.getSystemInfoSync().windowHeight - wpx1-48,
      height2: wx.getSystemInfoSync().windowHeight - wpx2-48
    });
  },
  noneEnoughPeople(){
    return;
  },
  // 去审批管理
  tocheck_order(){
    app.tocheck_manage(this.data.check_order_ids,this.data.purchase_order_ids,this.data.receipt_order_ids);
  },
  close_opera(){
    this.setData({
      isopera:false
    });
  },
  // 页面刷新
  PurchasingList(){
    var that = this;
    app.AppHttp("api/Order/getOrderDetailPurchase", {
      client_id:app.globalData.loginInfo.id
    }, function (res) {
      app.globalData.caigou_shua=0;
      var orderCount=0;
      if(res.data.supplier_list.length>0){
        for(var i=0;i<res.data.supplier_list.length;i++){
          orderCount+=res.data.result[res.data.supplier_list[i].id].length; 
        };
      };
      if(res.data.result[0]){
        orderCount+=res.data.result[0].length;
      };
      that.setData({
        loading:0,
        orderCount:orderCount,
        none_supplier:res.data.none_supplier,  //存在未知供应商
        cate_list:res.data.supplier_list,
        info:res.data.result,
        isclasscheck: {},
        is_checked: {},
        shop_ids:[],
        shop_good_ids:[],
        true_nums:[],
        order_ids:res.data.ids,
        imgs:[],
        isChooseAll: false,
        checkCount: 0
      });
    });
  },
  // 去需求单页
  toneed_order(){
    app.globalData.toneed_order=1;   //导航停在新需求单
    wx.switchTab({
      url:'/pages/dingdan/dingdan'
    });
  },
  // 去采购池
  tocaigou_chi(){
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    });
    this.setData({
      status: 0,  
      page:1   
    });
    this.PurchasingList();
  },
  noneEnoughPeople(){
    return;
  },
  typeTap(event) {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    });
    if (event.currentTarget.dataset.index == 0) {
      this.setData({
        status: 0,  
        page:1,
        load:'加载中...',
        loading:1 //正在加载
      });
      this.PurchasingList();
    } else if (event.currentTarget.dataset.index > 0) {
      this.setData({
        helist:[],
        datas:[],
        status:event.currentTarget.dataset.index,
        page:1,
        load:'加载中...',
        loading:1, //正在加载  
        isopera:false
      });
      app.globalData.task.abort();  
      this.ShouHuoCount();
      this.updateList();
    };
  },
  /**
   * 左侧切换选择
   * */
  leftClick(event) {
    var name = event.currentTarget.dataset.name;
    this.setData({
      leftChoose: event.currentTarget.dataset.id,
      toView: name
    });
  },
  // 商品数量弹窗
  open_tan_detail(event){
    return;
    var index=event.currentTarget.dataset.index; //类索引
    var eal=event.currentTarget.dataset.eal;   //商品索引
    util.showBuyModal(this);
    var item = this.data.info[index][eal];
    var tannum=item.nums;
    if(item.xiu_num){
      tannum=item.xiu_num;
    }
    this.setData({
      content:'',
      inputNum:'',
      cai_index:index,
      cai_eal:eal,
      isInput:true,
      tanimg: item.pr_cover?item.pr_cover:"",
      tanname: item.pr_name,
      tanunit: item.unit_name,
      tannum: tannum
    }); 
  },
  // 关闭键盘弹窗
  add_shop_cancel(){
    util.hideBuyModal(this,"isInput");
  },
  // 确定实际采购量
  shop_confirm(){
    if (this.data.inputNum == '') {
      this.setData({
        inputNum:this.data.tannum
      });
    }
    var info = this.data.info;
    if (this.data.inputNum>9999.99){
      wx.showToast({
        title: '数量过大',
      })
      return;
    };
    var item_id=info[this.data.cai_index][this.data.cai_eal].id; //当前商品的id;
    var true_nums=this.data.true_nums;  //选中商品的true_num数组
    if(this.data.is_checked[item_id]){  //当前商品是否为选中的状态
      var true_nums_index=this.data.shop_ids.indexOf(item_id);   //用id查询修改实采值的索引  
      true_nums[true_nums_index]=this.data.inputNum * 1;
    };
    info[this.data.cai_index][this.data.cai_eal].xiu_num=this.data.inputNum * 1;
    util.hideBuyModal(this,"isInput");
    this.setData({
      true_nums:true_nums,
      info:info
    });
  },
  // 键盘按键
  keyTap(e) {
    util.keyfunction(e,this,3);
    this.setValue();
  },
  setValue(){
    this.setData({
      inputNum: this.data.content
    });
  },
  /**
   * 单选采购池商品
   */
  checkboxMerge(e){
    var info = this.data.info;
    var index = e.currentTarget.dataset.index; //商品类别索引
    var eal = e.currentTarget.dataset.eal; //商品索引
    var shop_ids = this.data.shop_ids; //选中商品的id数组
    var shop_good_ids=this.data.shop_good_ids; //选中商品的good_id数组
    var imgs = this.data.imgs; //选中商品的img数组
    var true_nums=this.data.true_nums;  //选中商品的true_num数组
    var id = e.currentTarget.dataset.id; //商品id
    var good_id=e.currentTarget.dataset.good_id;  //商品good_id
    var img = e.currentTarget.dataset.img; //商品图片
    var true_num=e.currentTarget.dataset.true_num;  //商品的实际采购量
    var isclasscheck = this.data.isclasscheck; //类选是否选中
    var is_checked = this.data.is_checked;
    if (is_checked[id] == true) {
      var eal = shop_ids.indexOf(id);
      shop_ids.splice(eal,1);
      shop_good_ids.splice(eal, 1);
      imgs.splice(eal, 1);
      true_nums.splice(eal, 1);
      is_checked[id] = false;
    } else {
      shop_ids.push(id);
      shop_good_ids.push(good_id);
      true_nums.push(true_num);    
      if(img){
        imgs.push(img);
      }else{
        imgs.push('/upload/smallprogram/noshopimg.png');
      };
      is_checked[id] = true;
    };
    for (var j = 0; j < info[index].length; j++) {
      if (!is_checked[info[index][j].id]) {
        isclasscheck[index] = false
        break;
      } else {
        isclasscheck[index] = true
      }
    };
    if (shop_ids.length == this.data.orderCount) {
      this.setData({
        isChooseAll: true,
        is_checked: is_checked,
        checkCount: shop_ids.length,
        isclasscheck: isclasscheck
      })
    } else {
      this.setData({
        isChooseAll: false,
        is_checked: is_checked,
        checkCount: shop_ids.length,
        isclasscheck: isclasscheck
      })
    };
  },
  /**
   * 按类选择采购池商品
   */
  checkedboxclass(e) {
    var shop_ids = this.data.shop_ids; //选中商品的id数组
    var shop_good_ids=this.data.shop_good_ids; //选中商品的good_id数组
    var imgs = this.data.imgs; //选中商品的img数组
    var true_nums=this.data.true_nums;  //选中商品的true_num数组
    var index = e.currentTarget.dataset.index; //类别索引
    var item = e.currentTarget.dataset.item;
    var isclasscheck = this.data.isclasscheck;
    isclasscheck[index] = !isclasscheck[index];
    var is_checked = this.data.is_checked;
    if (isclasscheck[index] == true) {
      for (var i in item) {
        if (!is_checked[item[i].id]) {
          shop_ids.push(item[i].id);
          shop_good_ids.push(item[i].good_id);
          if(item[i].xiu_num){
            true_nums.push(item[i].xiu_num);
          }else{
            true_nums.push(item[i].nums);
          };     
          if(item[i].pr_cover){
            imgs.push(item[i].pr_cover);
          }else if(item[i].pr_cover==''){
            imgs.push('/upload/smallprogram/noshopimg.png');
          };
        }
        is_checked[item[i].id] = true;
      }
    } else {
      for (var i in item) {
        if (is_checked[item[i].id] == true) {
          var eal = shop_ids.indexOf(item[i].id);
          shop_ids.splice(eal, 1);
          shop_good_ids.splice(eal, 1);
          imgs.splice(eal, 1);
          true_nums.splice(eal, 1);
        }
        is_checked[item[i].id] = false;
      }
    };
    if (shop_ids.length == this.data.orderCount) {
      this.setData({
        isChooseAll: true
      })
    } else {
      this.setData({
        isChooseAll: false
      })
    };
    this.setData({
      is_checked: is_checked,
      isclasscheck: isclasscheck,
      checkCount: shop_ids.length
    });
  },
  /**
   * 全选采购池商品
   */
  allChoose(){
    var info = this.data.info;
    var isclasscheck = this.data.isclasscheck;
    var is_checked = this.data.is_checked;
    var isChooseAll = !this.data.isChooseAll;
    var shop_ids = []; //选中商品的id数组
    var shop_good_ids=[]; //选中商品的good_id数组
    var imgs = []; //选中商品img数组
    var true_nums=[];  //选中商品的true_num数组
    for (var i in info) {
      if (isChooseAll == true) {
        isclasscheck[i] = true;
        for (var j = 0; j < info[i].length; j++) {
          is_checked[info[i][j].id] = true;
          shop_ids.push(info[i][j].id);
          shop_good_ids.push(info[i][j].good_id);
          if(info[i][j].xiu_num){
            true_nums.push(info[i][j].xiu_num);
          }else{
            true_nums.push(info[i][j].nums);
          }; 
          if(info[i][j].pr_cover){
            imgs.push(info[i][j].pr_cover);
          }else{
            imgs.push('/upload/smallprogram/noshopimg.png');
          };
        }
      } else {
        isclasscheck[i] = false;
        for (var j = 0; j < info[i].length; j++) {
          is_checked[info[i][j].id] = false;
          var eal = shop_ids.indexOf(info[i][j].id);
          shop_ids.splice(eal, 1);
          shop_good_ids.splice(eal, 1);
          imgs.splice(eal, 1);
          true_nums.splice(eal, 1);
        }
      }
    };
    this.setData({
      isChooseAll: isChooseAll,
      imgs: imgs,
      shop_ids: shop_ids,
      shop_good_ids:shop_good_ids,
      true_nums:true_nums,
      is_checked: is_checked,
      isclasscheck: isclasscheck,
      checkCount: isChooseAll == true ? this.data.orderCount : 0
    });
  },
  /**
   * 操作点击
  */
  operate(){
    this.setData({
      isopera: !this.data.isopera
    })
  },
  // 采购池移动到
  super_move_conf(e){
    var that = this;
    var supplier_id=e.currentTarget.dataset.supplier_id;
    app.AppHttp('api/Order/setOrderDetailGoodBelongSupplier', {
      ids:that.data.shop_ids,
      b_supplier_id:supplier_id
    }, function (res) {
      that.PurchasingList();
      wx.showToast({
        title: res.data.msg
      });   
    });
    util.hideBuyModal(this,"issuper");
  },
  // 表单提交
  formSubmit(e){
    var that = this;
    var ids = that.data.shop_ids;
    var good_ids = that.data.shop_good_ids;
    var imgs = that.data.imgs.splice(0,3);
    var order_ids = that.data.order_ids;
    var true_nums=that.data.true_nums;
    if (ids.length != 0) {
      if (e.detail.target.dataset.type == "classify") { //归类到   
        util.showBuyModal(this);  
        var that = this;
        that.setData({
          issuper:true
        });
        if(that.data.good_dir_set<1){     //非企业版
          app.AppHttp('api/Client/SupplierList', {
            openid: app.globalData.openid,
            type: 1
          }, function (res) {
            if(res.data.status==-1){
              wx.showLoading({
                title: res.data.msg,
                duration:500
              });
              return;
            };
            var super_list=res.data.data[0]
            super_list = super_list.filter(function (item) {
              return (item.status==1 && item.is_zicai_supplier==0); //筛选正常且非自采的合作用户
            });
            that.setData({
              super_list:super_list
            });
          });   
        }else if(that.data.good_dir_set>=1){   //企业版
          app.AppHttp('api/Client/belongBusinessSupplier', {
            client_id:app.globalData.loginInfo.id
          }, function (res) {
            if(res.data.status==-1){
              wx.showLoading({
                title: res.data.msg,
                duration:500
              });
              return;
            }
            that.setData({
              super_list: res.data
            });
          }); 
        }
      }else if (e.detail.target.dataset.type == "caigoudan") { //生成采购单
        wx.navigateTo({
          url: '/pages/dingdan/dingdantianxie/dingdantianxie?shop_ids=' + JSON.stringify(ids)+'&type='+'caigou'+'&imgs='+ JSON.stringify(imgs)+'&order_ids='+order_ids+'&pr_num_str='+true_nums+'&is_enquiry='+0
        });
        that.PurchasingList();
      }else if(e.detail.target.dataset.type == "xunjiadan"){  //生成询价单
        wx.navigateTo({
          url: '/pages/dingdan/dingdantianxie/dingdantianxie?shop_ids=' + JSON.stringify(ids)+'&type='+'caigou'+'&imgs='+ JSON.stringify(imgs)+'&order_ids='+order_ids+'&pr_num_str='+true_nums+'&is_enquiry='+1
        });
        that.PurchasingList();
      };
      that.setData({
        isopera: false
      })
    } else {
      wx.showToast({
        title: '请至少选择一种商品',
        duration: 500,
        icon: "loading"
      })
    }
  },
  // 关闭收货提醒
  close_shou_hint(){
    this.setData({
      isshou_hint:false
    });
  },
  // 关闭审批提醒
  close_check_hint(){
    this.setData({
      is_check_hint:false
    });
  },
  // 采购单和入库列表
  updateList(){
    var that = this;
    if(that.data.page==1){
      wx.showLoading({
        title: that.data.load
      });
    };
    var start_time;
    var select_time=0;
    if(that.data.start_time==0){
      start_time=that.data.area_time;
    }else{
      start_time=Date.parse(new Date(that.data.start_time+'.00:00:00'));
    };
    var end_time = Date.parse(new Date(that.data.end_time+'.23:59:59'));
    if(start_time!=0){
      select_time=start_time+','+end_time
    };
    var up_index;
    var off_list=[];
    var settle_accounts=1;
    if(that.data.status==1){
      up_index=0;
      app.AppHttp('api/Client/getBelongClientEnquiry', {
        client_id:app.globalData.loginInfo.id,
        supplier_id: null,
        list_type:'order'
      }, function(res) {
        off_list=res.data.data;
        that.setData({
          off_list:res.data.data
        });
      });
    }else if(that.data.status==2){
      up_index=4;
    }else if(that.data.status==3){
      up_index=5;
    }else if(that.data.status==4){
      up_index=1;
    }else if(that.data.status==5){
      up_index=1;
      settle_accounts=2;
    };
    app.AppHttp('api/Order/latestOrder', {
      openid: app.globalData.openid,
      status: up_index,
      page:that.data.page,
      supplier_id:up_index==1?that.data.choose_supplier_id:0,
      mode:'caigou',
      select_time:up_index==1?select_time:0,
      // select_status:that.data.status==1?that.data.screen_type_index:-1001
      select_status:-1001,
      settle_accounts:settle_accounts
    }, function (res) {
      app.globalData.caigou_shua=0;
      if(res.data.status!=-1){
        var datas;
        if (that.data.page == 1){
          datas = res.data.list.list;
        }else{
          datas = that.data.datas.concat(res.data.list.list);
        };
        var update_order_list=res.data.update_order_list.list;
        update_order_list=update_order_list.concat(off_list);
        that.setData({
          loading:0,
          datas: datas,
          helist:update_order_list.concat(datas),
          page_count: res.data.list.page_count,
          order_list: res.data.update_order_list.list,
          auth_type: res.data.auth_type,
          ftotal_num:res.data.list.count,
          ftotal_money:res.data.list.zongJi
        });
      };
      wx.hideLoading();
    })   
  },
  // 打开筛选下拉
  open_screen(){
    var is_screen=this.data.is_screen;
    if(is_screen){
      util.hideBuyModal(this,"is_screen");
    }else{
      util.showBuyModal(this,-1);
      this.setData({
        is_screen:true
      });
    } 
  },
  // 关闭筛选下拉
  close_screen(){
    util.hideBuyModal(this,"is_screen");
  },
  // 选择供应商(采购单列表筛选)
  chose_super(){
    var that = this;
    util.showBuyModal(this);  
    that.setData({
      issuper:true
    });
    app.AppHttp("api/Order/getCBelongSupplier", {
      openid: app.globalData.openid
    }, function(res) { 
      // var superlist=[{"supplier_id":"0","company_name":"全部"}];
      that.setData({
        // superlist:superlist.concat(res.data.data)
        superlist:res.data.data
      });
    });
  },
  // 确定供应商
  super_conf(e){
    var supplier_id=e.currentTarget.dataset.supplier_id;
    var company_name=e.currentTarget.dataset.company_name;
    this.setData({
      z_choose_supplier_id:supplier_id,
      z_choose_supplier_name:company_name
    });
    util.hideBuyModal(this,"issuper");
  },
  // 关闭供应商
  close_super(){
    util.hideBuyModal(this,"issuper");
  },
  // 选择筛选类型
  chose_screen_type(e){
    this.setData({
      z_screen_type_index:e.currentTarget.dataset.index
    });
  },
  // 选择时间段
  chose_area(e){
    this.setData({
      z_screen_area_index:e.currentTarget.dataset.index,
      c_start_time:0,
      c_end_time:today,
      z_start_time:0,
      z_end_time:today
    });
  },
  // 打开时间弹窗
  chose_time(e){
    util.showBuyModal(this); 
    if(this.data.z_start_time==0){
      this.setData({
        z_start_time:today
      });
    };
    this.setData({
      showModalStatus:true
    });
  },
  // 关闭时间弹窗
  hideBuyModal(){
    util.hideBuyModal(this,"showModalStatus");
  },
  // 时间周期选中效果
  bcolor(e){
    var val_time=e.currentTarget.dataset.id==1?this.data.z_end_time:this.data.z_start_time;
    if(val_time!=0){
      val_time=(val_time).split('-');
      this.setData({
        value:[val_time[0] - 2010, val_time[1]-1, val_time[2] - 1]
      });
    }
    this.setData({
      time_type: e.currentTarget.dataset.id
    });
  },
  // 年、月、日改变
  bindChange(e){
    util.bindchange_time(e,this,date)
  },
  // 重置时间
  reset_time_but(){
    if(this.data.z_start_time!=0 || this.data.z_end_time!=today){
      this.setData({ 
        z_start_time:0, //选择开始时间
        z_end_time:today   //选择结束时间,默认今天
      });
    };    
  },
  // 确认并关闭选择时间周期弹窗
  queding(){
    if (Date.parse(new Date(this.data.z_end_time)) < Date.parse(new Date(this.data.z_start_time))){
      wx.showLoading({
        title: '时间不合理',
        duration:1000
      });
    }else{
      util.hideBuyModal(this,"showModalStatus");
      this.setData({
        z_screen_area_index:-1,
        c_start_time: this.data.z_start_time,  //开始时间
        c_end_time: this.data.z_end_time,    //结束时间
      });
    }  
  },
  // 筛选查看
  screen_queding(){
    util.hideBuyModal(this,"is_screen");
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    });
    var area_time = 0;
    if(this.data.z_screen_area_index==0){  //过去1天
      area_time = Date.parse(new Date(today))-3600000 * 24
    }else if(this.data.z_screen_area_index==1){  //过去1周
      area_time = Date.parse(new Date(today))-3600000 * 24*7
    }else if(this.data.z_screen_area_index==2){  //过去30天
      area_time = Date.parse(new Date(today))-3600000 * 24*30
    };
    this.setData({
      choose_supplier_id:this.data.z_choose_supplier_id,
      choose_supplier_name:this.data.z_choose_supplier_name,
      screen_type_index:this.data.z_screen_type_index,
      screen_area_index:this.data.z_screen_area_index,
      area_time:area_time,
      start_time: this.data.c_start_time,
      end_time: this.data.c_end_time,
      loading:1,
      load:'加载中...',
      page:1
    })
    this.updateList();
  },
  // 全部重置
  screen_reset_but(){
    util.hideBuyModal(this,"is_screen");
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    });
    this.setData({
      z_choose_supplier_id:0,
      z_screen_type_index:-1001,
      z_screen_area_index:-1,
      z_start_time:0,
      z_end_time:today,
      c_start_time:0,
      c_end_time:today,
      choose_supplier_id:0,
      choose_supplier_name:'',
      screen_type_index:-1001,
      screen_area_index:-1,
      area_time:0,
      start_time:0,
      end_time:today,
      loading:1,
      load:'加载中...',
      page:1
    });
    this.updateList();
  },
  // 去采购详情页
  toCaiDetail(event){
    var id = event.currentTarget.dataset.id;
    var index = event.currentTarget.dataset.index;
    let order_status = event.currentTarget.dataset.order_status;
    var is_cancel_apply = event.currentTarget.dataset.is_cancel_apply;
    var is_enquiry=event.currentTarget.dataset.is_enquiry;
    var is_created=event.currentTarget.dataset.is_created;
    var is_substitute=event.currentTarget.dataset.is_substitute;
    if(order_status==0 && is_substitute==1){
      wx.showModal({
        title: '提示',
        content: "此单为代下单，对方正在编辑订单中",
        showCancel: false,
        confirmText:"我知道了",
        success: function (res) {}
      });
      return;
    };
    var check_type;
    if(order_status<4){
      check_type='purchase_order';
    }else if(order_status>=4){
      check_type='receipt_order';
    };
    if (is_cancel_apply!=1){
      wx.navigateTo({
        url: '/pages/dingdan/order_detail/order_detail?id=' + id + "&order_status=" + order_status+ "&settitle=" + "采购单"+ "&title_index="+ 2+"&is_enquiry="+is_enquiry+"&is_created="+is_created+'&check_type='+check_type+"&d_index=" + index
      });
    }else{
      wx.showLoading({
        title: '等待对方确定',
        duration: 500
      });
    };
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady(){

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow(){
    var that = this;
    app.globalData.dingdan_show = 1;  
    var def_status=app.globalData.loginInfo.good_dir_set==2 && app.globalData.loginInfo.client_is_store==1 && app.globalData.loginInfo.purchase_type==2?1:0 //模认导航索引
    that.setData({
      good_dir_set:app.globalData.loginInfo.good_dir_set,    //目录类型
      client_is_store:app.globalData.loginInfo.client_is_store,  //<0未加入集团，==0加入集团无库 ,==1加入集团有库
      purchase_type:app.globalData.loginInfo.purchase_type,  //1门店采购，2总部采购
      def_status:def_status, //模认导航索引
      status:that.data.status>1 || that.data.status==1 && def_status==0?that.data.status:def_status
    });
    if(app.globalData.loginInfo.client_is_store<0 && app.globalData.type_level==1 || app.globalData.loginInfo.client_is_store==1){
      app.globalData.list =  dealers;
      if (typeof this.getTabBar === 'function' && this.getTabBar()) {
        this.getTabBar().setData({
          selected:3,
          list: app.globalData.list
        })
      };
    };
    if(app.globalData.caigou_shua==1){
      app.AppHttp("api/Order/daiShouHuoCount", { 
        client_id: app.globalData.loginInfo.id, 
      }, function (res) {
        that.setData({
          shou_hint_num:res.data.data
        });
        if(res.data.status==1){
          that.setData({
            isshou_hint:true,
            is_check_hint:true
          });
          setTimeout(function() {
            that.setData({
              isshou_hint:false
            });
          }, 5000);
        }       
      });
      app.get_count_list(that);
      if(this.data.status == 0){
        this.PurchasingList();
      }else if(this.data.status>0){   
        wx.pageScrollTo({
          scrollTop: 0,
          duration: 0
        });
        this.setData({
          page:1,
          load:'加载中...'
        });
        this.updateList();        
      }; 
    }  
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide(){

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload(){

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh(){
    app.globalData.caigou_shua=1;
    this.onShow();
    wx.stopPullDownRefresh();
  },
  // 收货提醒
  ShouHuoCount(){
    var that = this;
    app.AppHttp("api/Order/daiShouHuoCount", { 
      client_id: app.globalData.loginInfo.id, 
    }, function (res) {
      that.setData({
        shou_hint_num:res.data.data
      });       
    });      
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom(){
    if(this.data.status>0){
      if (this.data.page < this.data.page_count) {
        this.setData({
          loading:1,
          page: this.data.page + 1,
          load: '加载更多'
        });
        this.updateList();
      } else {
        this.setData({
          loading:0
        });
        wx.showToast({
          title: '没有了'
        })
      }
    };  
  }
})